package com.example.ppjoke.frame.dynamicnav;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.ppjoke.R;
import com.example.ppjoke.frame.dynamicnav.entity.FrameTabsEntity;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/***
 * 自定义bottom
 * 更具服务器下发的json字符串bottom
 * */
public class AppBottomView extends BottomNavigationView {
    private int[]icons = {R.drawable.icon_tab_home,R.drawable.icon_tab_sofa,R.drawable.icon_tab_publish,
            R.drawable.icon_tab_find,R.drawable.icon_tab_mine};
    public AppBottomView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        super(context, attrs);
        createItem();
    }

    @SuppressLint("RestrictedApi")
    private void createItem(){
        FrameTabsEntity frameTabsEntity=AppConfig.getInstance().frameTabsEntity;
         int[] colors={
                 Color.parseColor(frameTabsEntity.activeColor),
                 Color.parseColor(frameTabsEntity.inActiveColor),
         };
         int [][] state=new int[2][];
         state[0]=new int[]{android.R.attr.state_selected};
         state[1]=new int[]{-android.R.attr.state_selected};
        ColorStateList stateList=new ColorStateList(state,colors);
        setItemTextColor(stateList);
        setItemIconTintList(stateList);
        List<FrameTabsEntity.Tabs> tabs=frameTabsEntity.tabs;
        for (FrameTabsEntity.Tabs tab : tabs) {
            if (!tab.enable)
                continue;
            int id=readDestinationId(tab.pageUrl);
            if (id==-1){
                continue;
            }
            MenuItem item=getMenu().add(0,id,tab.index,tab.title);
            item.setIcon(icons[tab.index]);
        }
        int index=0;
        for (FrameTabsEntity.Tabs tab : tabs) {
            int size= ConvertUtils.dp2px(tab.size);
            BottomNavigationMenuView menuView= (BottomNavigationMenuView) getChildAt(0);
            BottomNavigationItemView itemView= (BottomNavigationItemView) menuView.getChildAt(index);
            itemView.setIconSize(size);
            itemView.setShifting(false);
            menuView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
            int tintColor;
            if (TextUtils.isEmpty(tab.title)){
                tintColor=Color.parseColor("#ff678f");
            }else {
                tintColor=Color.parseColor("#666666");
            }
            itemView.setIconTintList(ColorStateList.valueOf(tintColor));
            index++;
        }
//        设置tab默认选中
        if (frameTabsEntity.selectTab>=0){
            if (frameTabsEntity.tabs.get(frameTabsEntity.selectTab).enable){
                String url=frameTabsEntity.tabs.get(frameTabsEntity.selectTab).pageUrl;
                int id=readDestinationId(url);
                setSelectedItemId(id);
            }
        }
    }

    private int readDestinationId(String pageUrl){
        if (TextUtils.isEmpty(pageUrl)){
            return -1;
        }
        int id=AppConfig.getInstance().destinationMap.get(pageUrl).id;;
        return id;
    }


}
